package com.eurabia.eggchat.service.impl;

import com.eurabia.eggchat.VO.ResultVO;
import com.eurabia.eggchat.dao.SysEggchatUserDao;
import com.eurabia.eggchat.enums.LoginEnum;
import com.eurabia.eggchat.enums.ResultEnum;
import com.eurabia.eggchat.pojo.SysEggchatUser;
import com.eurabia.eggchat.pojo.form.SysEggchatUserForm;
import com.eurabia.eggchat.service.SysEggchatLoginService;
import com.eurabia.eggchat.utils.EncryptionUtil;
import com.eurabia.eggchat.utils.ResultVOUtil;
import com.eurabia.eggchat.utils.SessionUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * description:登陆、退出、注册、注销等方法
 * @author zqx
 * @date 2018/08/28
 */
@Service
@Slf4j
public class SysEggchatLoginServiceImpl implements SysEggchatLoginService{


    @Resource
    private SysEggchatUserDao sysEggchatUserDao;
    /**
     * 通过用户名和手机号查询用户 （登录）
     * @param form
     * @param request
     * @return
     */
    @Override
    @Transactional
    public ResultVO login(SysEggchatUserForm form,HttpServletRequest request) {
        ResultVO rv= new ResultVO();
        List<SysEggchatUser> users = sysEggchatUserDao.getByUserName(form);
        if(users != null && users.size()>0)
        {
            SysEggchatUser user = users.get(0);
            String passwordDb = user.getUserPass();
            //密码输入正确
            if(passwordDb.equals(EncryptionUtil.encryptionSHA256(form.getUserPass().trim())))
            {
                rv.setCode(String.valueOf(LoginEnum.LOGIN_SUCCESS.getCode()));
                rv.setMsg(LoginEnum.LOGIN_SUCCESS.getMessage());
                rv.setCount(users.size());
                SessionUtils.setSession(request,"user",user);
            }else if(users !=null && users.size()>1)
            {
                rv.setCode(String.valueOf(LoginEnum.LOGIN_MANY_USER.getCode()));
                rv.setMsg(LoginEnum.LOGIN_MANY_USER.getMessage());
                rv.setCount(users.size());
                log.error("【登录接口】出大事了，查询出多个相同用户，用户名为："+user.getUsername());
            }else
            {
                rv.setCode(String.valueOf(LoginEnum.LOGIN_FAIL.getCode()));
                rv.setMsg(LoginEnum.LOGIN_FAIL.getMessage());
                rv.setCount(users.size());
                log.error("【登录接口】用户不存在，用户名为："+user.getUsername());
            }
        }else
        {
            rv.setCode(String.valueOf(LoginEnum.LOGIN_FAIL.getCode()));
            rv.setMsg(LoginEnum.LOGIN_FAIL.getMessage());
            rv.setCount(users.size());
        }
        return rv;
    }

    /**
     * 退出方法
     * @param request
     * @return
     */
    @Override
    @Transactional
    public ResultVO loginOut(HttpServletRequest request) {
        //移除session
        SessionUtils.removeSession(request,"user");
        return new ResultVO(String.valueOf(LoginEnum.LOGINOUT_SUCCESS.getCode()),LoginEnum.LOGINOUT_SUCCESS.getMessage());
    }

    /**
     * 注册方法
     * @param form
     * @return
     */
    @Override
    @Transactional
    public ResultVO regist(final SysEggchatUserForm form) {
        ResultVO rv = new ResultVO();
        //用户名不可重复
        List<SysEggchatUser> userList = sysEggchatUserDao.getByUserName(form);
        if(userList.size()>0)
        {
            rv.setCode(String.valueOf(LoginEnum.REGIST_USERNAME_EXIST.getCode()));
            rv.setMsg(LoginEnum.REGIST_USERNAME_EXIST.getMessage());
            rv.setCount(userList.size());
        }else
        {
            form.setUserPass((EncryptionUtil.encryptionSHA256(form.getUserPass().trim())));
            int result = sysEggchatUserDao.regist(form);
            if(result>0)
            {
                rv.setCode(String.valueOf(LoginEnum.REGIST_SUCCESS.getCode()));
                rv.setMsg(LoginEnum.REGIST_SUCCESS.getMessage());
                rv.setCount(result);
            }else
            {
                rv.setCode(String.valueOf(LoginEnum.REGIST_FAIL.getCode()));
                rv.setMsg(LoginEnum.REGIST_FAIL.getMessage());
                rv.setCount(result);
                log.error("【注册接口】"+LoginEnum.REGIST_FAIL.getMessage());
            }
        }
        return rv;
    }

    /**
     * 更新密码
     * @param form
     * @param request
     * @return
     */
    @Override
    @Transactional
    public ResultVO updateUserPass(SysEggchatUserForm form,HttpServletRequest request) {
        ResultVO rv= new ResultVO();
        SysEggchatUser user = (SysEggchatUser) SessionUtils.getSession(request,"user");
        if(user == null)
        {
            rv.setCode(String.valueOf(LoginEnum.NO_USER_EXIST.getCode()));
            rv.setMsg(LoginEnum.NO_USER_EXIST.getMessage());
            log.error("【更新密码接口】"+LoginEnum.NO_USER_EXIST.getMessage());
        }
        BeanUtils.copyProperties(user,form);
        form.setUserPass(EncryptionUtil.encryptionSHA256(form.getUserPass().trim()));
        int result = sysEggchatUserDao.update(form);
        if (result>0)
        {
            rv.setCode(String.valueOf(LoginEnum.RESULT_SUCCESS.getCode()));
            rv.setMsg(LoginEnum.RESULT_SUCCESS.getMessage());
            rv.setCount(result);
        }else
        {
            rv.setCode(String.valueOf(LoginEnum.RESULT_FAIL.getCode()));
            rv.setMsg(LoginEnum.RESULT_FAIL.getMessage());
            rv.setCount(result);
            log.error("【更新密码接口】"+LoginEnum.RESULT_FAIL.getMessage());
        }
        return rv;
    }

    /**
     * 注销
     * @param id
     * @return
     */
    @Override
    public ResultVO cancel(Long id) {
        int result = sysEggchatUserDao.delete(String.valueOf(id));
        if(result>0)
        {
            //注销成功
            return ResultVOUtil.success();
        }else{
            //注销失败
            return ResultVOUtil.error(String.valueOf(ResultEnum.CANCEL_FAILED.getCode()),ResultEnum.CANCEL_FAILED.getMessage());
        }
    }
}
